<?php
class EmployeesController extends AppController {

	var $name = 'Employees';
	var $uses = array('Employee','TypeOfService','TypesOfSpaces');
	private $group;
	
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag','emplo'),
		'edit' => array('admin','manag','emplo'),
		'delete' => array('admin','manag'),
		'employee' => array('admin','manag','emplo'),
	);
	
	public $presetVars = array(
		array('field' => 'name', 'type' => 'like'),
		array('field' => 'surname', 'type' => 'like'),
		array('field' => 'address1', 'type' => 'like'),
		array('field' => 'address3', 'type' => 'like'),
		array('field' => 'address4', 'type' => 'like'),
		array('field' => 'address5', 'type' => 'like'),
		array('field' => 'phone1', 'type' => 'like'),
		array('field' => 'ssn_irs', 'type' => 'like'),
		array('field' => 'numer_id', 'type' => 'like'),
		array('field' => 'type_of_work_id', 'type' => 'value')
	);	
	
	public function beforeFilter(){
		parent::beforeFilter();	
		$this->group = $this->Session->read("Auth.User.group");
	}
		
	function index() {

		$this->Employee->recursive = 0;
		$employees = $this->paginate();
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		
		$this->set(compact('employees', 'typeOfWorks'));
		
	}
	
	public function find() {
		$this->Prg->commonProcess();
		$this->paginate['conditions'] = $this->Employee->parseCriteria($this->passedArgs);	
		$employees = $this->paginate();
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		
		$this->set(compact('employees', 'typeOfWorks'));
		
		$this->autoRender = false;
		$this->render('index');
	}
	
	function employee() {
		if($this->group == 'emplo'){
			$id = $this->Employee->field('id',array('user_id'=>$this->Session->read("Auth.User.id")));
			$this->redirect(array('action'=>'view',$id));
		}else{
			$this->Session->setFlash(__("Invalid employee", true));
			$this->redirect(array('controller'=>'user','action'=>'login'));
		}	
	}

	function view($id = null) {
		if (!$id) {			
			$this->Session->setFlash(__("Invalid employee", true));
			$this->redirect(array('action' => 'index'));
		}
		
		// Components closing Series
		$this->Series->close();
		
		// slowniki zdefiniowane w Orders
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		
		$employee = $this->Employee->read(null, $id);
		$typeOfServices = $this->TypeOfService->find('list');
		$typeOfSpaces = $this->TypesOfSpaces->find('list');
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		$EmployeeSalary = $this->Employee->OrdersSeries->find('first',array('conditions'=>array('OrdersSeries.employee_id'=>$id,'OrdersSeries.orders_billing_id'=> array(0,null)),'fields'=>array('SUM(OrdersSeries.employee_salary) AS employee_salary'), 'group' => 'OrdersSeries.employee_id'));
		$this->set(compact('employee', 'typeOfServices','typeOfSpaces','typeOfWorks','EmployeeSalary'));
		
	}

	function add() {
		if (!empty($this->data)) {
			$this->Employee->set($this->data);
			if ($this->Employee->validates($this->data)) {
				$this->data['User']['email'] = $this->data['Employee']['email'];
				$this->data['User']['password'] = $this->Auth->password($this->data['Employee']['password']);	
				$this->data['User']['status'] = 1;
				$this->data['User']['group'] = 'emplo';		
						
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->data['Employee']['user_id'] = $this->User->getLastInsertID();
					$this->data['Employee']['registration_date'] = date('Y-m-d H:i:s');				
					$this->Employee->create();
					if ($this->Employee->save($this->data)) {
						$this->Session->setFlash(__("The employee has been saved", true), 'flash_succes');
						$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__("The employee could not be saved. Please, try again.", true), 'flash_error');
					}
				} else {
					$this->Session->setFlash(__("The employee could not be saved. Please, try again.", true), 'flash_error');
				}

			} else {
				$this->Session->setFlash(__("The employee could not be saved. Please, try again.", true), 'flash_error');	
//				$this->redirect($this->referer());
			}					
		}
		$users = $this->Employee->User->find('list');
		$orders = $this->Employee->Order->find('list');
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		$this->set(compact('users', 'orders','typeOfWorks'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__("Invalid employee", true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			
			if ($this->Employee->save($this->data)) {
				$user_email['User']['id'] = $this->data['Employee']['user_id'];
				$user_email['User']['email'] = $this->data['User']['email'];
				$this->Employee->User->id = $user_email['User']['id'];
				$this->Employee->User->save($user_email);
				$this->Session->setFlash(__("The employee has been saved", true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__("The employee could not be saved. Please, try again.", true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Employee->read(null, $id);
		}
		$users = $this->Employee->User->find('list');
		$orders = $this->Employee->Order->find('list');
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		$this->set(compact('users', 'orders','typeOfWorks'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid id for employee", true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Employee->delete($id)) {
			$this->Session->setFlash(__("Employee deleted", true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__("Employee was not deleted", true));
		$this->redirect(array('action' => 'index'));
	}


}
?>